package com.cr.socket.class3;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;

public class UDPClient {
    public static void main(String[] args) throws Exception{
        InetAddress addr= InetAddress.getLoopbackAddress();
        try(DatagramSocket socket=new DatagramSocket()){
            socket.connect(addr,9090);
            byte [] data="time".getBytes(StandardCharsets.UTF_8);
            DatagramPacket packet=new DatagramPacket(data,data.length);
            socket.send(packet);
            System.out.println("Data was send...");
            Thread.sleep(1000);
            byte [] buffer=new byte[1024];
            DatagramPacket resp=new DatagramPacket(buffer,buffer.length);
            socket.receive(resp);
            byte [] resData=resp.getData();
            String resText=new String(resData,0,resp.getLength());
            System.out.println("Response :"+ resText);
        }
    }
}
